package com.coach.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.map.HashedMap;
import org.javasimon.aop.Monitored;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.coach.entity.Class;
import com.coach.entity.ClassToCoach;
import com.coach.entity.ClassToStudent;
import com.coach.entity.Coach;
import com.coach.dao.ClassDAO;
import com.coach.dao.ClassToCoachDAO;
import com.coach.dao.ClassToStudentDAO;
import com.coach.dao.CoachDAO;
import com.coach.entity.ClassDTO;
import com.coach.util.BaseResponse;

@Transactional
@Service
@Monitored
public class ClassService {
	
	@Autowired
	private ClassDAO classDAO;
	@Autowired
	private ClassToCoachDAO classToCoachDAO;
    @Autowired
	private CoachDAO coachDAO;
    @Autowired
    private ClassToStudentDAO classToStudentDAO;

	
	public BaseResponse<ClassDTO> getClassList(Map<String, Object> map){		
		BaseResponse<ClassDTO> response = new BaseResponse<ClassDTO>();
	    try {
	        List<Class> list = classDAO.getClassList(map);
	        List<ClassDTO> list1=new ArrayList<ClassDTO>();
	        for(int i=0;i<list.size();i++){
	        	ClassDTO cd=new ClassDTO();
	        	cd.setCls(list.get(i));
	        	Map<String,Object> map1=new HashedMap();
	        	map1.put("id",list.get(i).getId() );
	        	List<ClassToCoach> ctc=classToCoachDAO.getListCoachId(map1);
	        	for(int j=0;j<ctc.size();j++){
	        		Map<String,Object> map2=new HashedMap();
	        	    map2.put("id",ctc.get(j).getCoachId());
	        		List<Coach> co=coachDAO.getCoachList(map2);
	        		cd.setCoaches(co);	        	  
	        	}
	        	list1.add(cd);
	        }
	        response.setSuccess(true);
	        response.setData(list1);
	    } catch(Exception e) {
	        e.printStackTrace();
	        response.setSuccess(false);
	    }
	    return response;
	}
	
	public BaseResponse<Class> getUserClassList(Map<String,Object> map){
		BaseResponse<Class> response=new BaseResponse<Class>();
		try{
			List<ClassToStudent> list=classToStudentDAO.getListClassToStudent(map);
			List<Class> list1=new ArrayList<Class>();
			for(int i=0;i<list.size();i++){
				Class cs=classDAO.getClassById(list.get(i).getClassId());
				list1.add(cs);
			}
			response.setSuccess(true);
			response.setData(list1);
		}catch(Exception e){
			e.printStackTrace();
			response.setSuccess(false);
		}
		
		return response;
	}
	
	
	public BaseResponse<Class> getCoachClassList(Map<String,Object> map){
		BaseResponse<Class> response=new BaseResponse<Class>();
		try{
			List<ClassToCoach> list=classToCoachDAO.getListClassToCoach(map);
			List<Class> list1=new ArrayList<Class>();
			for(int i=0;i<list.size();i++){
				Class cs=classDAO.getClassById(list.get(i).getClassId());
				list1.add(cs);
			}
			response.setSuccess(true);
			response.setData(list1);
		}catch(Exception e){
			e.printStackTrace();
			response.setSuccess(false);
		}
		
		return response;
	}
	
	
	public BaseResponse<ClassToCoach> checkClassToCoach(Map<String,Object> map){
		BaseResponse<ClassToCoach> response=new BaseResponse<ClassToCoach>();
		try{
			List<ClassToCoach> list=classToCoachDAO.checkClassToCoachExist(map);
			if(list.size()>0){
			response.setSuccess(true);
			response.setData(list);
			}else{
				response.setSuccess(false);
				response.setData(null);
			}
		}catch(Exception e){
			e.printStackTrace();
			response.setSuccess(false);
		}
		return response;
	}
	
	
	
	public BaseResponse<Class> addClass(Class c){
		BaseResponse<Class> response=new BaseResponse<Class>();
		try{
			classDAO.addClass(c);
			response.setSuccess(true);
			response.setData(null);
		}catch(Exception e){
			e.printStackTrace();
			response.setSuccess(false);
		}
		
		return response;
	}
	
	public BaseResponse<Class> updateClass(Class c){
		BaseResponse<Class> response=new BaseResponse<Class>();
		try{
		   classDAO.updateClass(c);
		   response.setSuccess(true);
		   response.setData(null);
		}catch(Exception e){
			e.printStackTrace();
			response.setSuccess(false);
		}
		return response;
	}
	
	public BaseResponse<Class> deleteClass(Class c){
		BaseResponse<Class> response=new BaseResponse<Class>();
		try{
			classDAO.deleteClass(c);
			response.setSuccess(true);
			response.setData(null);
		}catch(Exception e){
			e.printStackTrace();
			response.setSuccess(false);	
		}
		
		return response;
	}
	
	public BaseResponse<ClassDTO> getClassById(int id){
		BaseResponse<ClassDTO> response=new BaseResponse<ClassDTO>();
		try{
			classDAO.getClassById(id);
			response.setSuccess(true);
			response.setData(null);
		}catch(Exception e){
			e.printStackTrace();
			response.setSuccess(false);
		}
		
		return response;
	}
	
	
	public BaseResponse<Class> getClassListPage(Map<String,Object> map){
		BaseResponse<Class> response=new BaseResponse<Class>();
		try{
			classDAO.getClassListPage(map);
			response.setSuccess(true);
			response.setData(null);
		}catch(Exception e){
			e.printStackTrace();
			response.setSuccess(false);
		}
		
		return response;
	}
	

}
